<?php

App::uses('CrudController', 'Controller');

class LinksController extends CrudController
{
    public $_model = 'Link';

    public function admin($workstationConfigId, $workstationId, $lineId, $areaId, $moeId) {
        $this->request->allowMethod('get');
        $this->loadModel('Area');
        $this->loadModel('Moe');
        $this->loadModel('Line');
        $this->loadModel('Workstation');
        $this->loadModel('WorkstationConfig');
        $appBosch = new stdClass();
        $appBosch->workstationConfigId = $workstationConfigId;
        $appBosch->workstationId = $workstationId;
        $appBosch->lineId = $lineId;
        $appBosch->areaId = $areaId;
        $appBosch->moeId = $moeId;
        $appBosch->workstationsConfigs = $this->WorkstationConfig->getPossibleDependencies($workstationConfigId);
        $description = $this->getDescription($workstationConfigId, $workstationId, $lineId, $areaId, $moeId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Dependencias'));
        $this->set('description', $description);
    }

    private function getDescription($workstationConfigId, $workstationId, $lineId, $areaId, $moeId) {
        $this->Moe->id = $moeId;
        $moe = $this->Moe->read();
        $this->Area->id = $areaId;
        $area = $this->Area->read();
        $this->Line->id = $lineId;
        $line = $this->Line->read();
        $this->Workstation->id = $workstationId;
        $workstation = $this->Workstation->read();
        $this->WorkstationConfig->id = $workstationConfigId;
        $workstationConfig = $this->WorkstationConfig->read();
        $parts = array(
            mb_strtoupper($moe['Moe']['name']),
            mb_strtoupper($area['Area']['name']),
            mb_strtoupper($line['Line']['name']),
            mb_strtoupper($workstation['Workstation']['description']),
            mb_strtoupper($workstationConfig['WorkstationConfig']['name']),
        );
        return implode(' - ', $parts);
    }

    protected function getRecords($param) {
        $m = $this->_model;
        $records = $this->$m->getBySource($param);
        return $records;
    }

    protected function c($model) {
        return array(
            'source' => trim($model->source),
            'destination' => trim($model->destination),
        );
    }

    protected function u($model) {
        return array(
            'destination' => trim($model->destination),
        );
    }

}
